<template>
  <div class="app-container">
    <!-- 顶部搜索栏 -->
    <div class="header">
      <div class="header-content">
        <div class="logo-container">
          <img src="./logo.png" alt="宽可充 Pet supplies" class="logo" />
        </div>
        <div class="search-area">
          <div class="search-container">
            <div class="search-box">
              <input type="text" placeholder="搜索商品" v-model="searchQuery" @keyup.enter="searchProducts" />

              <button @click="searchProducts">
                <svg viewBox="0 0 24 24" width="30" height="30">
                  <path
                    fill="currentColor"
                    d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 0 0 1.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 0 0-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 0 0 5.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"
                  />
                </svg>
              </button>
              <a
                ><svg viewBox="0 0 24 24" width="25" height="25">
                  <path
                    fill="currentColor"
                    d="M9 2L7.17 4H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2h-3.17L15 2H9zm3 15c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5z"
                  />
                  <circle cx="12" cy="12" r="3" />
                </svg>
              </a>
            </div>
          </div>
          <!-- 添加搜索提示文字 -->
          <div class="search-hints">
            <span @click="searchHintClick('狗粮')">狗粮</span>
            <span @click="searchHintClick('猫砂')">猫砂</span>
            <span @click="searchHintClick('猫粮')">猫粮</span>
            <span @click="searchHintClick('宠物玩具')">宠物玩具</span>
            <span @click="searchHintClick('宠物衣服')">宠物衣服</span>
            <span @click="searchHintClick('磨牙玩具')">磨牙玩具</span>
            <span @click="searchHintClick('棉质玩具')">棉质玩具</span>
          </div>
        </div>
      </div>
    </div>
    <div class="main-wrapper">
      <div class="main-content">
        <!-- 侧边分类栏 -->
        <div class="sidebar">
          <h2 class="sidebar-title">商品分类</h2>
          <ul class="category-list">
            <li
              v-for="category in categories"
              :key="category.id"
              :class="{ active: activeCategory === category.id }"
              @click="changeCategory(category.id)"
            >
              {{ category.name }}
            </li>
          </ul>
        </div>

        <!-- 主内容区 -->
        <div class="content">
          <!-- 当前分类标题 -->
          <h2 class="current-category">{{ currentCategoryName }}</h2>

          <!-- 商品列表 -->
          <div class="product-list">
            <div class="product-item" v-for="product in filteredProducts" :key="product.id">
              <div class="product-image">
                <img :src="getImageUrl(product.image)" :alt="product.name" />
              </div>
              <div class="product-info">
                <div class="product-name">{{ product.name }}</div>
                <div class="product-sec">
                  <div class="product-price">¥{{ product.price }}</div>
                  <div class="product-sales">销量{{ product.sales }}+</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 右侧悬挂导航栏 -->
    <div class="right-nav">
      <router-link to="/">
        <div class="nav-item" :class="{ active: activeNav === 'home' }" @click="activeNav = 'home'">
          <svg viewBox="0 0 24 24" width="25" height="25">
            <path fill="currentColor" d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z" />
          </svg>
          <span>首页</span>
        </div></router-link
      >
      <router-link to="/service">
        <div class="nav-item" :class="{ active: activeNav === 'category' }" @click="activeNav = 'category'">
          <svg viewBox="0 0 24 24" width="25" height="25">
            <path
              fill="currentColor"
              d="M20 2H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h14l4 4V4c0-1.1-.9-2-2-2zm-2 12H6v-2h12v2zm0-3H6V9h12v2zm0-3H6V6h12v2z"
            />
          </svg>
          <span>客服</span>
        </div></router-link
      >
      <router-link to="/carts">
        <div class="nav-item" :class="{ active: activeNav === 'cart' }" @click="activeNav = 'cart'">
          <svg viewBox="0 0 24 24" width="25" height="25">
            <path
              fill="currentColor"
              d="M7 18c-1.1 0-1.99.9-1.99 2S5.9 22 7 22s2-.9 2-2-.9-2-2-2zM1 2v2h2l3.6 7.59-1.35 2.45c-.16.28-.25.61-.25.96 0 1.1.9 2 2 2h12v-2H7.42c-.14 0-.25-.11-.25-.25l.03-.12.9-1.63h7.45c.75 0 1.41-.41 1.75-1.03l3.58-6.49c.08-.14.12-.31.12-.48 0-.55-.45-1-1-1H5.21l-.94-2H1zm16 16c-1.1 0-1.99.9-1.99 2s.89 2 1.99 2 2-.9 2-2-.9-2-2-2z"
            />
          </svg>
          <span>购物车</span>
        </div></router-link
      >
      <router-link to="/personal-center">
        <div class="nav-item" :class="{ active: activeNav === 'user' }" @click="activeNav = 'user'">
          <svg viewBox="0 0 24 24" width="25" height="25">
            <path
              fill="currentColor"
              d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 3c1.66 0 3 1.34 3 3s-1.34 3-3 3-3-1.34-3-3 1.34-3 3-3zm0 14.2c-2.5 0-4.71-1.28-6-3.22.03-1.99 4-3.08 6-3.08 1.99 0 5.97 1.09 6 3.08-1.29 1.94-3.5 3.22-6 3.22z"
            />
          </svg>
          <span>我的</span>
        </div></router-link
      >
    </div>
  </div>
</template>

<script>
import { useRouter } from "vue-router";
import typeData from "./type.json";
export default {
  name: "ProductCategoryPage",
  setup() {},
  data() {
    return {
      searchQuery: "",
      activeCategory: 1,
      activeNav: "home",
      categories: typeData.categories,
      products: typeData.products,
    };
  },
  computed: {
    currentCategoryName() {
      const category = this.categories.find((c) => c.id === this.activeCategory);
      return category ? category.name : "全部商品";
    },

    filteredProducts() {
      // 按分类筛选
      let result = this.activeCategory === 1 ? this.products : this.products.filter((p) => p.category === this.activeCategory);

      // 按搜索词筛选
      if (this.searchQuery) {
        const query = this.searchQuery.toLowerCase();
        result = result.filter((p) => p.name.toLowerCase().includes(query));
      }

      return result;
    },
  },
  methods: {
    // 添加搜索提示点击方法
    searchHintClick(keyword) {
      this.searchQuery = keyword;
      this.searchProducts();
    },
    changeCategory(categoryId) {
      this.activeCategory = categoryId;
    },
    searchProducts() {
      // 搜索逻辑已在计算属性中处理
      console.log("搜索:", this.searchQuery);
    },
    getImageUrl(imageName) {
      return `/image/${imageName}`;
    },
  },
};
</script>

<style scoped>
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.app-container {
  font-family: "PingFang SC", "Helvetica Neue", Arial, sans-serif;
  background-color: #f5f5f5;
  min-height: 100vh;
  position: relative;
  margin: -7px;
}

/* 头部样式 */
.header {
  padding: 15px 0;
  position: sticky;
  top: 0;
  z-index: 100;
  height: 105px;
  background-color: #f5f5f5;
}

.header-content {
  width: 70%;
  margin: 0 auto;
  display: flex;
  align-items: center;
}

.logo-container {
  flex: 0 0 auto;
  margin-left: 120px;
}

.logo {
  height: 85px; /* 根据实际图片调整高度 */
  width: auto;
}

.search-container {
  width: 70%;
  margin: 0 auto;
  display: flex;
  justify-content: center;
  margin-left: 30px;
}
/* 搜索输入框 */
.search-box {
  width: 100%;
  display: flex;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  border: 3px solid #d9c596;
  background-color: #f4efd2;
  position: relative;
}

.search-box input {
  flex: 1;
  height: 45px;
  padding: 10px 15px;
  border: none;
  outline: none;
  font-size: 18px;
  background-color: #f4efd2;
}

.search-box button {
  margin: 5px;
  padding: 0px 15px;
  background-color: #dbdd90;
  color: #414233;
  width: 80px;
  font-size: 16px;
  border-radius: 10px;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.search-box a {
  position: absolute;
  right: 100px;
  top: 10px;
  cursor: pointer;
}

/* 新增搜索区域样式 */
.search-area {
  display: flex;
  flex-direction: column;
  width: 100%;
  margin-left: 50px;
}

/* 搜索提示文字样式 */
.search-hints {
  display: flex;
  justify-content: center;
  gap: 15px;
  margin-top: 8px;
  margin-left: 20px;
  flex-wrap: wrap;
  width: 500px;
}

.search-hints span {
  font-size: 12px;
  color: #666;
  cursor: pointer;
  padding: 2px 8px;
  border-radius: 12px;
  transition: all 0.2s;
}

.search-hints span:hover {
  background-color: #f0f0f0;
  color: #f4a46f;
}
/* 主内容区布局 */
.main-wrapper {
  width: 80%;
  margin: 15px auto 0;
  padding-right: 60px; /* 为右侧导航留出空间 */
}

.main-content {
  display: flex;
  background-color: #f5f5f5;
  min-height: calc(100vh - 120px);
  gap: 20px;
}

/* 侧边栏样式 */
.sidebar {
  width: 140px;
  height: 620px;
  background-color: #f7f3dd;
  padding: 15px 0;
  margin-right: -1px;

  position: sticky;
  top: 120px;
  border-radius: 10px;
  text-align: center;
}
.sidebar::after {
  content: "";
  position: absolute;
  top: 0;
  right: -1px;
  bottom: 0;
  width: 1px;
  background-color: #f5f5f5;
}
.sidebar-title {
  font-size: 18px;
  padding: 0 15px 10px;
  margin: 0;
  color: #666;
  font-weight: normal;
}

.category-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.category-list li {
  padding: 12px 15px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s;
  color: #333;
}

.category-list li:hover {
  background-color: #dcdbdb;
}

.category-list li.active {
  background-color: #f4a46f;
  color: white;
  position: relative;
}

.category-list li.active::after {
  content: "";
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 16px;
  background-color: white;
}

/* 主内容区样式 */
.content {
  flex: 1;
  padding: 15px;
  background-color: #f7f3dd;
  border-radius: 5px;
}

.current-category {
  font-size: 18px;
  margin: 0 0 15px;
  color: #333;
  font-weight: 500;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}

/* 商品列表样式 */
.product-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 15px;
}

.product-item {
  border-radius: 8px;
  padding: 10px;
  transition: all 0.3s;
  background-color: white;
  border: 1px solid #eee;
}

.product-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
}

.product-image {
  width: 100%;
  height: 120px;
  margin-bottom: 10px;
  border-radius: 5px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f5f5;
}
.product-image img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.product-name {
  font-size: 16px;
  font-weight: 700;
  margin-bottom: 8px;
  color: #333;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.product-sec {
  margin-top: 15px;
  display: flex;
  justify-content: space-between;
}

.product-price {
  font-size: 16px;
  font-weight: bold;
  color: #fe6500;
}

.product-sales {
  font-size: 12px;
  color: #8c8c8c;
  display: flex;
}

/* 右侧悬挂导航栏样式 */
.right-nav {
  position: fixed;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
  background-color: white;
  border-radius: 25px;
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
  padding: 15px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 110;
}

.right-nav .nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px 8px;
  color: #666;
  cursor: pointer;
  transition: all 0.3s;
  border-radius: 20px;
  margin: 5px 0;
  width: 60px;
}

.right-nav .nav-item:hover {
  background-color: #f5f5f5;
}

.right-nav .nav-item.active {
  color: #ff6700;
  background-color: rgba(255, 103, 0, 0.1);
}

.right-nav .nav-item svg {
  margin-bottom: 5px;
}

.right-nav .nav-item span {
  font-size: 14px;
}
.right-nav a {
  text-decoration: none;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .main-wrapper,
  .search-container {
    width: 95%;
  }

  .main-wrapper {
    padding-right: 0;
  }

  .search-box {
    width: 60%;
  }

  .sidebar {
    width: 100px;
  }

  .product-list {
    grid-template-columns: repeat(2, 1fr);
  }

  .right-nav {
    right: 10px;
    padding: 10px 5px;
  }

  .right-nav .nav-item {
    padding: 8px 5px;
    margin: 3px 0;
    font-size: 10px;
    width: auto;
    min-width: 40px;
    height: 40px;
    justify-content: center;
  }

  .right-nav .nav-item span {
    display: none;
  }
}

@media (max-width: 480px) {
  .main-wrapper,
  .search-container {
    width: 100%;
  }

  .main-content {
    flex-direction: column;
    gap: 0;
  }

  .sidebar {
    width: 100%;
    min-height: auto;
    display: flex;
    overflow-x: auto;
    padding: 10px 0;
  }

  .sidebar-title {
    display: none;
  }

  .category-list {
    display: flex;
    padding: 0 10px;
  }

  .category-list li {
    white-space: nowrap;
  }

  .category-list li.active::after {
    bottom: 0;
    top: auto;
    left: 50%;
    transform: translateX(-50%);
    width: 16px;
    height: 3px;
  }

  .product-list {
    grid-template-columns: 1fr;
  }

  .right-nav {
    right: 5px;
    padding: 8px 3px;
  }

  .right-nav .nav-item {
    padding: 5px 3px;
    margin: 2px 0;
    min-width: 36px;
    height: 36px;
  }

  .right-nav .nav-item svg {
    width: 16px;
    height: 16px;
    margin-bottom: 0;
  }
}
</style>
